import { SchemaManage } from '@/components/SchemaComponent';
import React, { useRef, useMemo, useEffect } from 'react';
import { connect } from 'umi';
import { ManageAPI } from '../common/type';
import { DetailManageTableSwitch } from '../mconfig';
import { DetailManageServices } from '../services';
import { PlusOutlined } from '@ant-design/icons';

const DetailManageView: React.FC<ManageAPI.IManageView> = ({ curRes, locationProps }) => {
  let {
    state: { record: detailInfo },
  } = locationProps;

  /**
   * 管理页services
   * */
  const { searchList, reloadRequest, pagination, loading, data } = DetailManageServices(curRes, detailInfo);

  /**
   * 管理页的ref
   * */
  const ManageViewRef: any = useRef();

  /**
   * 头部权限的切换
   * */
  const switchList: ManageAPI.IManageTabsSwitchType = useMemo(() => {
    // return ManageTabSwitch(curRes) as any;
    return [];
  }, [curRes]);

  /**
   * 头部切换方法
   * */
  const onSwitch = (key: string | undefined | number, searchValues: any, params: any) => {
    reloadRequest({ ...params, ...searchValues });
  };

  /**
   * 重置方法
   * */
  const onReset = (reqParams: ManageAPI.IAssetTableRequestType) => {
    reloadRequest({ ...reqParams });
  };

  /**
   * 搜索方法
   * */
  const onSearch = (reqParams: ManageAPI.IAssetTableRequestType) => {
    reloadRequest({ ...reqParams });
  };

  /**
   * 分页方法
   * */
  const onPageChange = (current: any, pageSize: any, params: any) => {
    reloadRequest({ pageSize, current, ...params });
  };

  /**
   * 左侧按钮
   * */
  const actionList: ManageAPI.IActionList = useMemo(() => {
    return [
      {
        title: '添加',
        visible: curRes?.[''] || false,
        type: 'primary',
        icon: <PlusOutlined />,
        onClick: () => {},
      },
    ];
  }, [curRes]);

  /**
   * Table的操作方法
   * */
  const getTableOperateFuncs = {};

  /**
   * Table的columns
   * */
  const getTableColumn: Record<string, any>[] = useMemo(() => {
    return DetailManageTableSwitch(curRes, getTableOperateFuncs);
  }, [curRes]);

  return (
    <SchemaManage
      ref={ManageViewRef}
      searchProps={{
        switchList,
        searchList,
        onSwitch,
        onReset,
        onSearch,
      }}
      actionList={actionList}
      tableProps={{
        columns: getTableColumn,
        dataSource: data?.rows,
        rowKey: 'id',
        loading: loading,
        pagination: pagination as any,
        onPageChange: onPageChange,
      }}
    />
  );
};

export default connect(({ baseLogin, navTab }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
  navTab,
}))(DetailManageView);
